﻿@page "/admin/listoperationrecords"
@attribute [Authorize(Roles = "Admin")]


@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<CnGalWebSite.ProjectSite.Shared.Components.Shared.TitleCard Title="操作记录版块" />

<MDCard>
    <DataTableCard TModel="OperationRecordOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <MIcon Color="success"
                       Class="mr-2"
                       OnClick="()=>LookItem(context.Item)">@IconType.Look.ToIconString()</MIcon>
            }
            else if (context.Header.Value == nameof(OperationRecordOverviewModel.Type))
            {
                @context.Item.Type.GetDisplayName()
            }
            else if (context.Header.Value == nameof(OperationRecordOverviewModel.UserId))
            {
                <CnGalWebSite.ProjectSite.Shared.Components.Admin.Users.UserChip Id="@context.Item.UserId" Name="@context.Item.UserName" />
            }
            else if (context.Header.Value == nameof(OperationRecordOverviewModel.Time))
            {
                @context.Item.Time.ToString("yyyy-MM-dd HH:mm")
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


@code {
    private string _baseUrl = "api/operationrecord/list";
    private string _name = "操作记录";

    private List<DataTableHeader<OperationRecordOverviewModel>> _headers = new List<DataTableHeader<OperationRecordOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(OperationRecordOverviewModel.Id)
          },
          new (){ Text= "类型", Value= nameof(OperationRecordOverviewModel.Type)},
          new (){ Text= "用户", Value= nameof(OperationRecordOverviewModel.UserId)},
          new (){ Text= "IP", Value= nameof(OperationRecordOverviewModel.IP)},
          new (){ Text= "Cookie", Value= nameof(OperationRecordOverviewModel.Cookie)},
          new (){ Text= "时间", Value= nameof(OperationRecordOverviewModel.Time)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    public void LookItem(OperationRecordOverviewModel model)
    {
        if (model.PageType == PageType.Project)
        {
            NavigationManager.NavigateTo($"/project/{model.PageId}");
        }
        else if (model.PageType == PageType.Stall)
        {
            NavigationManager.NavigateTo($"/stall/{model.PageId}");
        }
        else if (model.PageType == PageType.User)
        {
            NavigationManager.NavigateTo($"/user/space/{model.UserId}");
        }
    }
}
